<template>
  <el-dialog
    append-to-body
    :title="info"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    align="left"
    v-loading="currLoading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
    width="1200px">
    <el-form ref="form" v-if="dialogVisible" :model="form" label-width="200px" :rules="rules">
      <el-form-item label="公告-标题" align="left" prop="title">
        <el-input v-model="form.title" style="width: 200px"></el-input>
      </el-form-item>
      <el-form-item label="公告-内容" align="left" prop="content">
        <el-input type="textarea" v-model="form.content" style="width: 900px;" rows="10"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'CreateShopNoticeDialog',
  components: {},
  props:{
    successFunction:Function
  },
  data: function () {
    return {
      info:'创建',
      dialogVisible: false,
      currLoading: false,
      form: {
        id:0,
        title: '',
        content: '',
      },
      rules: {
        title: [
          {required: true, message: '请输入 公告标题', trigger: ['blur', 'change']},
          {min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: ['blur', 'change']}
        ],
        content: [
          {required: true, message: '请输入 公告内容', trigger: ['blur', 'change']},
          {min: 1, max: 5000, message: '长度在 1 到 5000 个字符', trigger: ['blur', 'change']}
        ],
      }
    }
  },
  mounted () {
  },
  methods: {
    open: function (infoData) {
      let $this=this
      $this.info='创建-公告'
      if (infoData.id) {
        $this.info='更新-公告'
        $this.form.id = infoData.id
        $this.form.title = infoData.title
        $this.form.content = infoData.content
      }else{
        $this.form={
          id:0,
          title: '',
          content: '',
        }
        if ($this.$refs.form){
          $this.$refs.form.resetFields()
        }
      }
      $this.dialogVisible = true
      $this.$forceUpdate()
    },
    submit: function () {
      let $this = this
      $this.$refs.form.validate((valid) => {
        if (valid) {
          var api = 'ShopNotice/create'
          if (this.form.id > 0) {
            api = 'ShopNotice/update'
          }
          $this.currLoading = true
          $this.MyRequest(this.ApiDoNameShop + api, $this.form, function () {
            $this.$message.success($this.info+'成功')
            $this.currLoading = false
            $this.form = {
              id:0,
              title: '',
              content: '',
            }
            $this.$props.successFunction()
            $this.dialogVisible=false
            $this.$forceUpdate()
          }, function () {
            $this.$message.success('操作失败，请重试')
            $this.currLoading = false
            $this.$forceUpdate()
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
